#include "EqualNode.h"
#include "..\Common\AssertHelper.h"

using namespace std;

namespace CExpression
{
	void EqualNode::Eval(ExecuteContext &context)
	{
		this->left->Eval(context);
		if(!context.ErrorOccured())
		{
			if(context.TempResultType()==ExecuteRunTimeType::Boolean)
			{
				bool operand1=context.GlobalTempBoolean();
				this->right->Eval(context);
				if(AssertHelper::AreSameType(context.TempResultType(),ExecuteRunTimeType::Boolean,context,TEXT("Logic-equal operator's operand must be same type.")))
				{
					context.SetGlobalTempBoolean(operand1==context.GlobalTempBoolean());
					context.SetTempResultType(ExecuteRunTimeType::Boolean);
				}
			}
			else if(context.TempResultType()==ExecuteRunTimeType::Double)
			{
				double operand1=context.GlobalTempNum();
				this->right->Eval(context);
				if(AssertHelper::AreSameType(context.TempResultType(),ExecuteRunTimeType::Double,context,TEXT("Logic-equal operator's operand must be same type.")))
				{
					context.SetGlobalTempBoolean(operand1==context.GlobalTempNum());
					context.SetTempResultType(ExecuteRunTimeType::Boolean);
				}
			}
			else
			{
				context.SetError();
				context.SetErrorMsg(TEXT("Equal operator's operand type is illegal."));
			}
		}
	}

	shared_ptr<Node> EqualNode::Clone(Expression &parent)
	{
		shared_ptr<Node> result(new EqualNode(this->left->Clone(parent),this->right->Clone(parent)));
		return result;
	}
}